﻿using DreamStore.WebShop.Models;
using JetBrains.Annotations;
using Orchard;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.UI.Notify;

namespace DreamStore.WebShop.Drivers
{
    [UsedImplicitly]
    public class PayPalApiSettingsPartDriver : ContentPartDriver<PayPalApiSettingsPart>
    {
        public Localizer T { get; set; }
        private readonly INotifier _notifier;
        private const string TemplateName = "Parts/PayPalApiSettings";

        public PayPalApiSettingsPartDriver(INotifier notifier)
        {
            _notifier = notifier;
            T = NullLocalizer.Instance;
        }

        protected override DriverResult Editor(PayPalApiSettingsPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_PayPalApiSettings",
                                () => shapeHelper.EditorTemplate(
                                    TemplateName: TemplateName,
                                    Model: part,
                                    Prefix: Prefix)).OnGroup("PayPal");
        }

        protected override DriverResult Editor(PayPalApiSettingsPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
        {
            if (updater.TryUpdateModel(part, Prefix, null, null))
            {
                _notifier.Information(T("PayPal API settings saved successfully"));
            }
            else
            {
                _notifier.Error(T("Error saving PayPal API settings!"));
            }
            return Editor(part, shapeHelper);
        }
    }
}